﻿<%@ WebHandler Language="C#" Class="wxMiniProgramLogin" %>
using Models;
using System;
using System.Web;
using Newtonsoft.Json;
using System.Linq;
using Newtonsoft.Json.Linq;
using System.Collections;

public class wxMiniProgramLogin : IHttpHandler {
    public void ProcessRequest (HttpContext context) {
        context.Response.ContentType = "text/plain";

        string postdata=Utils.PostInput(context);
        JObject jo = (JObject)JsonConvert.DeserializeObject(postdata);
        var js_code = jo["goods_list"];
        string authHeader = context.Request.Headers["token"];//Header中的token
        if (string.IsNullOrEmpty(authHeader))
        {
            HttpContext.Current.Response.Write("parameter error");
            return;
        }


        decimal goods_amount = 0;

        using (Entities bll = new Entities())
        {
            ArrayList listjson = new ArrayList();
            foreach (var item in js_code)
            {


                int gid=Convert.ToInt32(item["goods_id"]);
                var x = bll.S_Goods.FirstOrDefault(p => p.GID ==gid && p.IsLock != true);
                var GetImgs = methods.GetImgs(x.GImg);

                var gooddetail = new
                {
                    stock = x.GCounts,   //库存
                    id = x.GID,
                    title = x.GName,
                    content = x.GContent,
                    l = x.GName,
                    x.SCCLASSID,
                    images = GetImgs,
                    image = GetImgs[0],
                    original_price = x.GPriceYJ,
                    price = x.GPrice,
                    sales = x.GSales,
                    x.GSort,
                    current_sku_price = new {
                        goods_id= x.GID,
                        price = x.GPrice,
                    }

                };
                int num =Convert.ToInt32(item["goods_num"]);
                goods_amount += Convert.ToDecimal(x.GPrice)*num;
                decimal myprice = Convert.ToDecimal(item["goods_price"]);


                listjson.Add( new
                {
                    discount_fee = 0,
                    dispatch_amount = 0,
                    dispatch_id = 1,
                    dispatch_type = "express",
                    goods_amount = 11,
                    goods_id =gid,
                    goods_num = item["goods_num"],
                    goods_original_amount = 16,
                    goods_price =myprice,
                    score_amount = 0,
                    sku_price_id =myprice,
                    store_id = 0,
                    detail = gooddetail,

                });
            }




            var code = 1;
            var msg = "商品详情";
            var time = DateTime.Now;


            var data = new
            {
                goods_original_amount=goods_amount,  //总价
                goods_amount=goods_amount,   //总价
                dispatch_amount="0.00",
                total_amount=goods_amount,//总价
                total_fee=goods_amount,//总价
                discount_fee="0.00",
                coupon_fee="0.00",
                activity_type="",
                score_amount=0,
                new_goods_list=listjson,
                need_address=1
            };

            context.Response.Write(JsonConvert.SerializeObject(new { code,msg,time,data}));

        }

        HttpContext.Current.Response.End();

    }



    public bool IsReusable {
        get {
            return false;
        }
    }

}